home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / xmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-28  |  1.6 KB  |  75 lines

  1. /* xmalloc.c -- malloc with out of memory checking
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21.  
  22. #ifdef STDC_HEADERS
  23. #include <stdlib.h>
  24. #else
  25. char *malloc ();
  26. char *realloc ();
  27. void free ();
  28. #endif
  29.  
  30. #include "system.h"
  31. #include "logging.h"
  32.  
  33. static void
  34. mallocfailed()
  35. {
  36.   dprintf(L_FATAL, "malloc failed -- exiting\n");
  37. }
  38.  
  39. /* Allocate N bytes of memory dynamically, with error checking.  */
  40.  
  41. void *
  42. xmalloc (n)
  43.      unsigned n;
  44. {
  45.   char *p;
  46.  
  47.   p = malloc (n);
  48.   if (p == 0)
  49.     mallocfailed();
  50.   return p;
  51. }
  52.  
  53. /* Change the size of an allocated block of memory P to N bytes,
  54.    with error checking.
  55.    If P is NULL, run xmalloc.
  56.    If N is 0, run free and return NULL.  */
  57.  
  58. void *
  59. xrealloc (p, n)
  60.      void *p;
  61.      unsigned n;
  62. {
  63.   if (p == 0)
  64.     return xmalloc (n);
  65.   if (n == 0)
  66.     {
  67.       free (p);
  68.       return 0;
  69.     }
  70.   p = realloc (p, n);
  71.   if (p == 0)
  72.     mallocfailed();
  73.   return p;
  74. }
  75.